package com.cy.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.study.common.Response;
import com.cy.study.req.*;
import com.cy.study.res.CourseDetailRes;
import com.cy.study.res.CoursePageRes;
import com.cy.study.res.UserCourseDetailRes;
import com.cy.study.res.UserCoursePageRes;
import com.cy.study.service.CourseService;
import com.cy.study.service.MediaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import static com.cy.study.common.CommonConstant.FILE_PATH;
import static com.cy.study.common.CommonConstant.HEADER_PATH;

/**
 * 课程控制器
 */
@RequiredArgsConstructor
@RequestMapping("/course")
@RestController
@CrossOrigin
@Slf4j
public class CourseController {

    private final CourseService courseService;
    private final MediaService mediaService;

    /**
     * 管理员添加课程
     * @param req
     * @return
     */
    @PostMapping("/addCourse")
    public Response<Integer> addCourse(@RequestBody CourseAddReq req){
        Integer id = courseService.addCourse(req);
        return Response.success(id);
    }

    /**
     * 管理员添加小节
     * @param req
     * @return
     */
    @PostMapping("/addMedia")
    public Response<Integer> addMedia(@RequestBody MediaAddReq req){
        Integer id = mediaService.addMedia(req);
        return Response.success(id);
    }

    /**
     * 管理员添加小节视频，在添加小节时先调用这个
     * @param file
     * @return
     */
    @PostMapping("/uploadMedia")
    @ResponseBody
    public Response<String> uploadMedia(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()){
            return Response.serverError("文件上传失败");
        }
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //生成文件名称通用方法
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        Random r = new Random();
        StringBuilder tempName = new StringBuilder();
        tempName.append(sdf.format(new Date())).append(r.nextInt(100)).append(suffixName);
        String newFileName = tempName.toString();
        try {
            //保存文件
            byte[] bytes = file.getBytes();
            Path path = Paths.get(HEADER_PATH+newFileName);
            Files.write(path,bytes);
        }catch (IOException e){
            e.printStackTrace();
        }
        return Response.success(tempName.toString());
    }

    /**
     * 管理员删除小节
     * @param req
     * @return
     */
    @PostMapping("/deleteMedia")
    public Response deleteMedia(@RequestBody MediaDeleteReq req){
        mediaService.deleteMedia(req);
        return Response.success();
    }

    /**
     * 管理员删除课程
     * @param req
     * @return
     */
    @PostMapping("/deleteCourse")
    public Response deleteCourse(@RequestBody CourseDeleteReq req){
        courseService.deleteCourse(req);
        return Response.success();
    }

    /**
     * 管理员课程分页列表
     * @param req
     * @return
     */
    @PostMapping("/pageCourse")
    public Response<Page<CoursePageRes>> pageCourse(@RequestBody CoursePageReq req){
        Page<CoursePageRes> res = courseService.pageCourse(req);
        return Response.success(res);
    }

    /**
     * 管理员课程详情
     * @param courseId
     * @return
     */
    @GetMapping("/detail/{courseId}")
    public Response<CourseDetailRes> detailCourse(@PathVariable Integer courseId){
        CourseDetailRes res = courseService.detailCourse(courseId);
        return Response.success(res);
    }


    ///-----------------------用户端


    /**
     * 用户端课程列表
     * @param req
     * @return
     */
    @PostMapping("/userCoursePage")
    public Response<Page<UserCoursePageRes>> userCoursePage(@RequestBody UserCoursePageReq req){
        Page<UserCoursePageRes> res = courseService.userCoursePage(req);
        return Response.success(res);
    }

    /**
     * 用户端课程详情
     */
    @GetMapping("/userCourseDetail/{courseId}")
    public Response<UserCourseDetailRes> userCourseDetail(@PathVariable Integer courseId){
        UserCourseDetailRes res = courseService.userCourseDetail(courseId);
        return Response.success(res);
    }


}
